package com.ruoyi.common.im.service;

import io.rong.RongCloud;
import io.rong.methods.user.onlinestatus.OnlineStatus;
import io.rong.models.Result;
import io.rong.models.response.CheckOnlineResult;
import io.rong.models.response.TokenResult;
import io.rong.models.response.UserGroupQueryResult;
import io.rong.models.response.UserResult;
import io.rong.models.user.UserModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * im用户服务
 *
 * @author jzy
 * @date 2021/11/13
 */
@Slf4j
@Service
public class IMUserService {

    @Autowired
    private RongCloud rongCloud;

    /**
     * 注册用户，生成用户在融云的唯一身份标识 Token
     * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/user/user.html#register
     *
     * @param userId   用户id
     * @param nickName 昵称
     * @param avatar   头像
     */
    public TokenResult registerUser(String userId, String nickName, String avatar) {
        UserModel user = new UserModel()
                .setId(userId)
                .setName(nickName)
                .setPortrait(avatar);
        TokenResult result = null;
        try {
            result = rongCloud.user.register(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 刷新用户信息方法
     * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/user/user.html#refresh
     *
     * @param userId   用户id
     * @param nickName 用户昵称
     * @param avatar   游湖头像
     * @return
     */
    public Result updateUser(String userId, String nickName, String avatar) {
        UserModel user = new UserModel()
                .setId(userId)
                .setName(nickName)
                .setPortrait(avatar);
        Result result = null;
        try {
            Result update = rongCloud.user.update(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 查询用户
     * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/user/user.html#get
     *
     * @param userId 用户id
     * @return
     */
    public UserResult getUser(String userId) {
        UserModel user = new UserModel()
                .setId(userId);
        UserResult userResult = null;
        try {
            userResult = rongCloud.user.get(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userResult;
    }

    /**
     * 查询用户所在群组
     * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/user/user.html#get
     *
     * @param userId 用户id
     * @return
     */
    public UserGroupQueryResult userGroupList(String userId) {
        UserModel user = new UserModel()
                .setId(userId);
        UserGroupQueryResult userGroupResult = null;
        try {
            userGroupResult = rongCloud.user.getGroups(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userGroupResult;
    }

    /**
     * 查询用户在线状态
     *
     * @param userId
     * @return
     */
    public int userOnlineStatus(String userId) {
        UserModel user = new UserModel().setId(userId);
        int status = 0;
        try {
            CheckOnlineResult check = rongCloud.user.onlineStatus.check(user);
            if ("1".equals(check.getStatus())) {
                status = 1;
            } else {
                status = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return status;
    }

}